package com.thrd.taiexhibition.activity

import android.animation.ValueAnimator.AnimatorUpdateListener
import android.os.Bundle
import android.widget.TextView
import com.airbnb.lottie.LottieAnimationView
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.thrd.basicframework.utils.CommonDialog
import com.thrd.basicframework.utils.LogUtil
import com.thrd.taiexhibition.BaseApplication
import com.thrd.taiexhibition.R
import com.thrd.taiexhibition.bean.BackActivity
import com.thrd.taiexhibition.bean.BeanSystemInfo
import com.thrd.taiexhibition.presener.MainPresener
import com.thrd.taiexhibition.view.MainView


@Route(path = "/app/main",group = "app")
class MainActivity : BackActivity() ,MainView{
    private lateinit var mLottieAnimationView:LottieAnimationView
    private lateinit var mMainPresener:MainPresener
    private  var isAnimention=false


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        mMainPresener= MainPresener(this)
        mMainPresener.getSystemInfo()
        mLottieAnimationView  = findViewById(R.id.lottie_view) as LottieAnimationView;
        // mLottieAnimationView.setImageAssetsFolder("images/")
        mLottieAnimationView.setAnimation("huaian.json")
        mLottieAnimationView.playAnimation()
        mLottieAnimationView.addAnimatorUpdateListener(AnimatorUpdateListener { valueAnimator -> // 判断动画加载结束
            LogUtil.e("MainActivity","animatedFraction=${valueAnimator.animatedFraction}")
            if (valueAnimator.animatedFraction == 1f) {
                isAnimention=true;
                setHome()
            }
        })



        // val sss=LetterUtil.getFirstLetter("北京");
//        ARouter.getInstance().build(JumpUtils.RICH_TEXT).withLong(JumpUtils.RICH_ID, 1)
//            .navigation()

//            ARouter.getInstance().build("/app/home") .navigation()
//        ARouter.getInstance().build("/app/text") .navigation()
//        finish()


    }

    override fun onDestroy() {
        super.onDestroy()
        mLottieAnimationView?.cancelAnimation()

    }
    private  var mBeanSystemInfo: BeanSystemInfo? = null


    override fun onSuccess(mBeanSystemInfo: BeanSystemInfo?) {
        if (mBeanSystemInfo != null) {
            this.mBeanSystemInfo= mBeanSystemInfo
            val application=this.application as BaseApplication?
            application?.beanSystemInfo=this.mBeanSystemInfo;
            setHome()
        }


    }

    override fun onError(error: String?, type: Int) {
        setHome();
    }


    fun setHome(){
        if(isAnimention&&mBeanSystemInfo!=null){
            ARouter.getInstance().build("/app/home") .navigation()
//                ARouter.getInstance().build("/app/text") .navigation()
//            Log.e("dpi",""+resources.getDimensionPixelOffset(R.dimen.qb_px_1));
            finish()
        }else if(mBeanSystemInfo==null){
            showDataStatus()
        }
    }


    private var dialog: CommonDialog?=null

    /**
     * 弹出重试界面
     */
    fun showDataStatus(){
        if(dialog==null){
            var  builder = CommonDialog.Builder(this)
            dialog = builder!!
                .setView(R.layout.dialog_error) // .setStyle(R.style.MyDialogStyle)
                .setCancelTouchout(false)
                .setHeightdp(300)
                .setWidthdp(300)
                .build()
        }
        var mEtry= dialog?.findViewById<TextView>(R.id.tv_tv_etry);
        mEtry?.setOnClickListener {
            mMainPresener.getSystemInfo()
            dialog?.dismiss()
        }
        dialog?.show()
    }
}


